package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import entity.TbUser;

public class UserDAO {
	
	public List<TbUser> findAll(){
		Session session=HibernateUtils.getSession();
		String hql="from TbUser";
		Query query = session.createQuery(hql);
		List<TbUser> list = query.list();
		return list;
	}
	
	public void save(TbUser model) {
		Session session=HibernateUtils.getSession();
		session.persist(model);
	}
	
	public TbUser findById(Integer id) {
		Session session=HibernateUtils.getSession();
		TbUser user=(TbUser) session.get(TbUser.class, id);
		return user;
	}
	
	public TbUser findByName(String name) {
		String hql="from TbUser t where t.name=?";
		Session session=HibernateUtils.getSession();
		Query query = session.createQuery(hql);
		query.setString(0, name);
		TbUser user = (TbUser) query.uniqueResult();
		return user;
	}
	
	public List findBySex(String sex){
		String hql="from TbUser t where t.sex=?";
		Session session=HibernateUtils.getSession();
		Query query=session.createQuery(hql);
		query.setString(0, sex);
		List list = query.list();
		return list;
	}
}
